package com.yugao.fintech.antelope.mps.core.sender;

import com.alibaba.fastjson2.JSONObject;
import com.yugao.fintech.antelope.mps.api.constatnts.ChannelCodeEnum;
import com.yugao.fintech.antelope.mps.api.constatnts.ChannelTypeEnum;
import com.yugao.fintech.antelope.mps.api.constatnts.MsgPushStatus;
import com.yugao.fintech.antelope.mps.api.model.content.MsgContent;
import com.yugao.fintech.antelope.mps.api.model.content.template.TemplateVarData;
import com.yugao.fintech.antelope.mps.core.PushTaskContext;
import com.yugao.fintech.antelope.mps.core.channel.ChannelClientManager;
import com.yugao.fintech.antelope.mps.core.channel.client.WxMpMsgClient;
import com.yugao.fintech.antelope.mps.core.parser.MpTemplateParser;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 微信消息推送
 */
@Slf4j
@Component
public class WxMpSender implements MsgSender {
    @Autowired
    private ChannelClientManager channelClientManager;

    @Override
    public void sendMessage(PushTaskContext context) {
        context.setChannelCode(ChannelCodeEnum.WX_MP);
        MsgContent msgContent = (MsgContent) context.getMsgContent();
        WxMpMsgClient wxMpMsgClient = (WxMpMsgClient) channelClientManager.getChannelClient(context.getChannelType());
        context.setChannelCode(wxMpMsgClient.channelCode());

        WxMpService wxMpService = wxMpMsgClient.getWxMpService();
        WxMpConfigStorage wxMpConfigStorage = wxMpMsgClient.getWxMpConfigStorage();
        MpTemplateParser templateConfig = (MpTemplateParser) context.getTplConfig();

        log.info("accessToken: {}", wxMpConfigStorage.getAccessToken());
        try {
            // 创建模板消息，设置模板id、指定模板消息要发送的目标用户
            WxMpTemplateMessage wxMpTemplateMessage = WxMpTemplateMessage.builder()
                    .templateId(templateConfig.getTemplateId())
                    .toUser(msgContent.getToUser())
                    .url(msgContent.getPageUrl())
                    .build();

            // 填充模板消息中的变量
            for (TemplateVarData tpl : templateConfig.getVarData().values()) {
                wxMpTemplateMessage.addData(new WxMpTemplateData(tpl.getKey(), String.valueOf(tpl.getValue()), tpl.getColor()));
            }

            // 发送模板消息，返回消息id
            String msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("msgId", msgId);
            context.setPushResult(jsonObject.toJSONString());
        } catch (Exception e) {
            context.setPushStatus(MsgPushStatus.FAIL);
            context.setPushResult(e.getMessage());
            throw new BizException(-1, e.getMessage());
        }
    }

    @Override
    public ChannelTypeEnum channelType() {
        return ChannelTypeEnum.WX_MP;
    }
}
